package com.miao.goods.controller;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;


import com.alibaba.fastjson.TypeReference;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.miao.commonn.vo.BargainVo;
import com.miao.commonn.vo.BordersVo;
import com.miao.commonn.vo.RecordingVo;
import com.miao.goods.config.MD5Util;
import com.miao.goods.config.OssUtils;
import com.miao.goods.feignbo.FeignBargain;
import com.miao.goods.feignbo.FeignBoder;
import org.omg.CORBA.FREE_MEM;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.data.redis.core.BoundValueOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.miao.goods.entity.UserEntity;
import com.miao.goods.service.UserService;
import com.miao.commonn.utils.PageUtils;
import com.miao.commonn.utils.R;
import org.springframework.web.multipart.MultipartFile;


/**
 * 
 *
 * @author xk
 * @email 2337156522@QQ.com
 * @date 2022-02-22 16:32:45
 */
@RestController
@RequestMapping("goods/user")
public class UserController {
    @Autowired
    private UserService userService;

    @Autowired
    OssUtils ossUtils;

    @Autowired
    FeignBoder feignBoder;

    @Autowired
    FeignBargain feignBargain;

    @Autowired
    RedisTemplate redisTemplate;
    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = userService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info")
    @Transactional
    public R info(@RequestBody  UserEntity userEntity){
        QueryWrapper<UserEntity> w = new QueryWrapper<>();
        w.eq("nickname",userEntity.getNickname());

        UserEntity user = userService.getOne(w);

        if (user!=null){
            if (user.getPassword().equals(MD5Util.md5Code(userEntity.getPassword(),2))){
                BoundValueOperations uid1 = redisTemplate.boundValueOps("uid");

                Integer o = (Integer) uid1.get();
                if (o!=null){
                    R r = feignBoder.info1(userEntity.getBoid());

                    BordersVo borders = (BordersVo) r.getData("borders",new TypeReference<BordersVo>(){});
                    if (borders!=null){
                        List<RecordingVo> findlist = feignBargain.findlist(userEntity.getBoid());
                        if (findlist.size()>0){
                            R r1 = feignBargain.info1(borders.getBid());
                            BargainVo data = r1.getData("bargain",new TypeReference<BargainVo>() {
                            });
                            BigDecimal price = null;
                            for (RecordingVo recordingVo : findlist) {
                                if (recordingVo.getUid2().equals(user.getUid()) && recordingVo.getGid().equals(borders.getGoodsId())){

                                    return R.error("您已砍过价");
                                }else {
                                    if (data.getBrules()==2){
                                        price = price.add(recordingVo.getRprice());
                                    }
                                }
                            }




                            RecordingVo recordingVo = new RecordingVo();

                            if (data.getBrules()==1&& borders.getCountorlow()>0){
                                BigDecimal subtract = data.getGprice().subtract(data.getBjprice());

                                BigDecimal bigDecimal = new BigDecimal(data.getCountorlow().toString());
                                BigDecimal divide = subtract.divide(bigDecimal);

                                recordingVo.setBoid(userEntity.getBoid());
                                recordingVo.setGid(borders.getGoodsId());
                                recordingVo.setRprice(divide);
                                if (userEntity.getUid1()!=null){
                                    recordingVo.setUid1(userEntity.getUid1());
                                }
                                recordingVo.setUid2(user.getUid());
                                recordingVo.setRdate(new Date());

                                feignBargain.save(recordingVo);

                                borders.setCountorlow(borders.getCountorlow()-1);
                                borders.setShiPrice(borders.getShiPrice().subtract(divide));

                                feignBoder.update(borders);

                                return R.ok().put("ok",1);
                            }



                        }else {
                            R r1 = feignBargain.info1(borders.getBid());
                            BargainVo data = r1.getData("bargain",new TypeReference<BargainVo>() {
                            });
                            if (data.getBrules().equals(1) && borders.getCountorlow()>0){
                                BigDecimal subtract = data.getGprice().subtract(data.getBjprice());

                                BigDecimal bigDecimal = new BigDecimal(data.getCountorlow().toString());
                                BigDecimal divide = subtract.divide(bigDecimal);
                                RecordingVo recordingVo = new RecordingVo();
                                recordingVo.setBoid(userEntity.getBoid());
                                recordingVo.setGid(borders.getGoodsId());
                                recordingVo.setRprice(divide);
                                if (userEntity.getUid1()!=null){
                                    recordingVo.setUid1(userEntity.getUid1());
                                }
                                recordingVo.setUid2(user.getUid());
                                recordingVo.setRdate(new Date());

                                feignBargain.save(recordingVo);

                                borders.setCountorlow(borders.getCountorlow()-1);
                                borders.setShiPrice(borders.getShiPrice().subtract(divide));

                                feignBoder.update(borders);

                                return R.ok().put("ok",1);
                            }

                        }
                    }
                }
                user.setLastLoginData(new Date());
                user.setLoginCount(user.getLoginCount()+1);

                userService.updateById(user);
                BoundValueOperations uid = redisTemplate.boundValueOps("uid");

                uid.set(user.getUid(),30, TimeUnit.MINUTES);

                return R.ok().put("uid",user.getUid());
            }
        }


        return R.error("用户名或面错误");
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody UserEntity user){


        return userService.regirant(user);
    }

    @RequestMapping("/findby")
    public UserEntity findby(@RequestParam Integer uid){


        return  userService.getById(uid);
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody UserEntity user){
		userService.updateById(user);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Integer[] uids){
		userService.removeByIds(Arrays.asList(uids));

        return R.ok();
    }

    @RequestMapping("upload")
    public R upload(MultipartFile file){
        R r = ossUtils.uploadFile(file);


        return r;
    }



}
